package com.xbongbong.paas.handle;

import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.elasticsearch.model.PaasFormDataEsModel;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.factory.OptionFactory;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.FormDataListSearchHelp;
import com.xbongbong.paas.help.FormDataValidateProductHelp;
import com.xbongbong.paas.help.analysis.data.FormDataDistributorAnalysisDataHelp;
import com.xbongbong.paas.help.analysis.data.FormDataListNoSubFormAnalysisDataHelp;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.pro.formexplain.pojo.HandleExplainPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.IndexTypeModel;
import com.xbongbong.util.SaasLinkProductUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 吴峰
 * @version v1.0
 * @date 2020/3/24 16:21
 * @since v1.0
 */
@Component
public abstract class AbstractWebDataAnalysisHandle {

    @Resource
    private OptionFactory optionFactory;
    @Resource
    private FormDataListSearchHelp formDataListSearchHelp;
    @Resource
    private PaasFormDataEsModel paasFormDataEsModel;
    @Resource
    private FormDataListNoSubFormAnalysisDataHelp formDataListNoSubFormAnalysisDataHelp;
    @Resource
    private FormDataValidateProductHelp formDataValidateProductHelp;
    @Resource
    private FormDataDistributorAnalysisDataHelp formDataDistributorAnalysisDataHelp;
    @Resource
    private IndexTypeModel indexTypeModel;

    /**
     * 解析数据
     * @author 吴峰
     * @date 2020/3/24 16:36
     * @param formDataListDTO
     * @return void
     * @throws XbbException 业务异常
     */
    public abstract PaasFormDataEsListVO analysisData(FormDataListDTO formDataListDTO) throws XbbException;

    protected PaasFormDataEsListVO getEsList(FormDataListDTO formDataListDTO) throws XbbException{

        // 数据相关拼装
        HandleExplainPojo handleExplainPojo = formDataListSearchHelp.handleExplain(formDataListDTO);
         List<FieldAttrEntity> explainList = handleExplainPojo.getExplainList();
        PaasFormEntityExt form = handleExplainPojo.getForm();
        // 获取需要查询的字段，不再查询自定义子表单字段；
        List<String> fieldList = formDataListDTO.getFieldList();
        if (CollectionsUtil.isNotEmpty(fieldList)) {
            fieldList.addAll(handleExplainPojo.getFieldList());
            formDataListDTO.setFieldList(fieldList);
        } else {
            formDataListDTO.setFieldList(handleExplainPojo.getFieldList());
        }

        //数据查询
        IndexTypeEnum indexTypeEnum = indexTypeModel.getIndexType(formDataListDTO.getCorpid(), formDataListDTO.getBusinessType(), formDataListDTO.getSaasMark());
        PaasFormDataEsListVO paasFormDataEsListVO = paasFormDataEsModel.list(formDataListDTO, indexTypeEnum);
        paasFormDataEsListVO.setUpdateTime(handleExplainPojo.getUpdateTime());
        paasFormDataEsListVO.setForm(form);
        paasFormDataEsListVO.setHeadList(explainList);
        paasFormDataEsListVO.setExplainMap(formDataListDTO.getExplainMap());
        if ( Objects.equals(formDataListDTO.getFromExport(), BasicConstant.ONE) || Objects.equals(formDataListDTO.getFromExport(), BasicConstant.TWO) ) {
            return paasFormDataEsListVO;
        }
        // 经销商区域视图处理
        if (Objects.equals(formDataListDTO.getIsDistributorView(), BasicConstant.ONE)) {
            formDataListDTO.setPlatform(PlatFormEnum.DINGTALK.getValue());
        }
        if ( PlatFormEnum.WEB.getValue().equals(formDataListDTO.getPlatform()) ) {
            //设置阶段预警颜色部分数据
            formDataListSearchHelp.setWarningColorData(formDataListDTO, paasFormDataEsListVO);
            webDataAnalysis(formDataListDTO, paasFormDataEsListVO);
        } else if ( PlatFormEnum.DINGTALK.getValue().equals(formDataListDTO.getPlatform()) ) {
            paasFormDataEsListVO = formDataListSearchHelp.appDataAnalysis(formDataListDTO, paasFormDataEsListVO);
        }
        /* ==================== 上部按钮权限和下部按钮权限 ==================== */
        formDataListSearchHelp.setButtons(formDataListDTO, paasFormDataEsListVO);
        //表单冻结和隐藏列设置（在按钮设置后处理，防止操作列无法冻结和隐藏）
        formDataListSearchHelp.setFixedAndInvisible(formDataListDTO, paasFormDataEsListVO);
        return paasFormDataEsListVO;
    }

    /**
     * web端解析
     * @param formDataListDTO
     * @param formDataListVO
     * @throws XbbException
     */
    public void webDataAnalysis(FormDataListDTO formDataListDTO,PaasFormDataEsListVO formDataListVO) throws XbbException {
        // 数据解析
         Integer businessType = formDataListDTO.getBusinessType();

        if (Objects.equals(businessType, XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode()) || Objects.equals(businessType, XbbRefTypeEnum.PRODUCT.getCode())
                || Objects.equals(businessType, XbbRefTypeEnum.CONTRACT.getCode()) || Objects.equals(businessType,XbbRefTypeEnum.COMPETITOR.getCode())) {
            // 客户、合同、产品、竞争对手
            formDataListNoSubFormAnalysisDataHelp.analysisDataList(formDataListDTO, formDataListVO);
        } else {
            // else 所有解析都走这边除了以上的业务类型
            formDataDistributorAnalysisDataHelp.analysisDataList(formDataListDTO, formDataListVO);
        }
        List<FieldAttrEntity> explainList = formDataListVO.getHeadList() == null ? new ArrayList<>() : formDataListVO.getHeadList();
        //关联产品关联显示字段列表页解析，放在这里是因为移动端不需要，暂时
        SaasLinkProductUtil.productLinkShowField(explainList);
        formDataListVO.setHeadList(explainList);
        // 设置操作方式
        AbstractOptionHandle abstractOptionHandle = optionFactory.getByBusinessType(formDataListDTO.getBusinessType(), formDataListDTO.getSaasMark());
        abstractOptionHandle.setOptionsInExplain(formDataListDTO, explainList, formDataListVO);
        //web端金额千份位解析,不放入底层以免移动端多次解析失败 单独拎出来
        formDataListNoSubFormAnalysisDataHelp.analysisAmountOfThousandBits(formDataListVO, formDataListDTO.getAnalysisAmountOfThousandBitsWithOriginalValue());
        // 处理列表页字段排序(列排序)
        formDataListSearchHelp.explainFieldSort(formDataListDTO, formDataListVO);
    }
}
